package com.mybatis.service.shop.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.mybatis.basic.entity.AjaxJson;
import com.mybatis.basic.service.impl.BasicServiceImpl;
import com.mybatis.dao.shop.GoodsTypeDao;
import com.mybatis.entity.shop.GoodsType;
import com.mybatis.service.shop.GoodsTypeService;

@Service
public class GoodsTypeServiceImpl extends BasicServiceImpl<GoodsType> implements GoodsTypeService{

	private static final Logger Log = Logger.getLogger(GoodsTypeServiceImpl.class);

	@Autowired
	private GoodsTypeDao goodsTypeDao;

	@Override
	public AjaxJson add(GoodsType goodsType,Integer level) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			if(null!=level && level==1){
				goodsType.setParentId(0);
			}
			goodsType.setCreateTime(new Date());
			goodsType.setEditTime(new Date());
			super.insertOne(goodsType);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序出错，添加失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson queryOne(Integer id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			GoodsType g = super.selectById(new GoodsType(id));
			if(null==g)new Exception();
			System.err.println(g);
			ajaxJson.setObj(g);
			ajaxJson.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("查询失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson queryListByPage(GoodsType goodsType) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			//PageHelper.startPage(page, rows);
			//List<GoodsType> list = goodsTypeDao.pageList(goodsType);
			List<GoodsType> list = super.selectAll();
			ajaxJson.setObj(eachMenu(0,list));
			ajaxJson.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序出错，查询失败");
		}
		return ajaxJson;
	}

	//循环菜单
	private List<HashMap<String, Object>> eachMenu(int parentId,List<GoodsType> list){
		List<HashMap<String, Object>> result = new ArrayList<>();
		for (GoodsType g : list) {
			HashMap<String, Object> t = JSONObject.parseObject(JSONObject.toJSONString(g),new TypeReference<HashMap<String, Object>>(){});
			int parent_id = Integer.valueOf(t.get("parentId").toString());
			int id = Integer.valueOf(t.get("id").toString());

			if(parent_id==parentId){
				t.put("children", eachMenu(id, list));
				result.add(t);
			}
		}
		return result;
	}

	@Override
	public AjaxJson del(Integer id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = super.deleteById(new GoodsType(id));
			ajaxJson.setSuccess(result!=0?true:false);
			ajaxJson.setMsg(result!=0?"删除成功":"删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，操作失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson mod(GoodsType goodsType,Integer level) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			if(null!=level && level==1){
				goodsType.setParentId(0);
			}
			goodsType.setEditTime(new Date());
			int result = super.updateNotNull(goodsType);
			ajaxJson.setSuccess(result!=0?true:false);
			ajaxJson.setMsg(result!=0?"操作成功":"操作失败");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，操作失败");
		}
		return ajaxJson;
	}
	
}
